package indi.zhifa.recipe.bailan.framework.auth.dao.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import indi.zhifa.recipe.bailan.framework.auth.dao.mapper.AuthNodeMapper;
import indi.zhifa.recipe.bailan.framework.auth.dao.service.IAuthNodeDbService;
import indi.zhifa.recipe.bailan.framework.auth.entity.po.AuthNodeEntity;
import indi.zhifa.recipe.bailan.framework.mysql.entity.annotations.ZfDbService;
import indi.zhifa.recipe.bailan.framework.mysql.service.impl.ZfDbServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import java.io.Serializable;
import java.util.List;

@CacheConfig(cacheNames = "sys_auth_node")
@ZfDbService(name = "AuthNodeEntity")
public class AuthNodeDbServiceImpl extends ZfDbServiceImpl<AuthNodeMapper, AuthNodeEntity> implements IAuthNodeDbService {

    @Cacheable(key = "#pDomainId+'-'+#pCode", condition = "null!=#result")
    @Override
    public Long findByDomainIdAndCode(Long pDomainId, String pCode) {
        LambdaQueryWrapper<AuthNodeEntity> queryWrapper = Wrappers.<AuthNodeEntity>lambdaQuery()
                .eq(AuthNodeEntity::getDomainId,pDomainId)
                .eq(AuthNodeEntity::getCode,pCode);
        AuthNodeEntity authNodeEntity = findOne(queryWrapper);
        if(null == authNodeEntity){
            return null;
        }
        return authNodeEntity.getId() ;
    }

    @Cacheable(key = "#pDomainId")
    @Override
    public List<AuthNodeEntity> listByDomainId(Long pDomainId) {
        LambdaQueryWrapper<AuthNodeEntity> queryWrapper = Wrappers.<AuthNodeEntity>lambdaQuery()
                .eq(AuthNodeEntity::getDomainId,pDomainId);
        return list(queryWrapper);
    }

    @Override
    public List<AuthNodeEntity> listByParentId(Long pParentId) {
        LambdaQueryWrapper<AuthNodeEntity> queryWrapper = Wrappers.<AuthNodeEntity>lambdaQuery()
                .eq(AuthNodeEntity::getParentId,pParentId);
        List<AuthNodeEntity> children = list(queryWrapper);
        return children;
    }

    @Override
    public List<AuthNodeEntity> listByBeginWith(String pCascade) {
        LambdaQueryWrapper<AuthNodeEntity> queryWrapper = Wrappers.<AuthNodeEntity>lambdaQuery()
                .likeRight(AuthNodeEntity::getCascade,pCascade);
        List<AuthNodeEntity> descendants = list(queryWrapper);
        return descendants;
    }

    @Cacheable(key = "#pId")
    public AuthNodeEntity check(Serializable pId){
        return super.check(pId);
    }

    @CachePut(key = "#pId")
    public AuthNodeEntity savePull(Long pId, AuthNodeEntity pEntity){
        return super.savePull(pId,pEntity);
    }

    @CachePut(key = "#pId")
    public AuthNodeEntity updatePull(Long pId, AuthNodeEntity pEntity){
        return super.updatePull(pId,pEntity);
    }

    @CacheEvict(cacheNames = {"#pId","#result.domainId+'-'+#result.code"})
    public AuthNodeEntity deleteById(Serializable pId){
        return super.deleteById(pId);
    }

}
